package com.iot.inter.interfaceprocessing.newinterface;

import com.iot.inter.entity.common.OrderPaymentManager;
import com.iot.inter.entity.newpojo.Constant;
import com.iot.inter.entity.newpojo.TurntoInterfaceUtils;
import com.iot.inter.entity.newpojo.request.RequestBody.OrderMonthlyPackageRequestBody;
import com.iot.inter.entity.newpojo.request.RequestHeader;
import com.iot.inter.entity.newpojo.request.subRequest.OrderMonthlyPackageRequest;
import com.iot.inter.entity.newpojo.response.subResponse.OrderMonthlyPackageResponse;
import com.iot.inter.entity.newrequest.order.batchorder.request.RequestDeviceList;
import com.iot.inter.utils.HttpClientTool;
import com.iot.inter.utils.JaxbUtil;
import org.apache.log4j.Logger;

import java.util.List;

/**
 * Demo 去北京下实际的单时的实现类
 *
 * @author lm
 * @date 2018/08/31
 */
public class TurnToOrderService {
    private static Logger logger = Logger.getLogger(TurnToOrderService.class);

    /***
     * 老版的订购套餐
     * @param deviceId
     * @param globalOrder
     * @param packageName
     * @param packageCode
     * @param packageType
     * @param orderPeriod
     * @param amount
     * @param secret
     * @param partnerCode
     * @return
     */
    public OrderPaymentManager orderPackage(String deviceId, String globalOrder, String packageName, String packageCode, String packageType, String orderPeriod, String amount, String secret, String partnerCode) {

        OrderPaymentManager user = new OrderPaymentManager();
        try {
            // 请求参数的处理
            OrderMonthlyPackageRequestBody body = new OrderMonthlyPackageRequestBody();
            body.setDeviceId(deviceId);
            body.setGlobalOrder("0");
            body.setPackageName(packageName);
            body.setPackageCode(packageCode);
            body.setPackageType(packageType);
            body.setOrderPeriod(orderPeriod);
            body.setPayAmount(amount);
            //未支付
            body.setPayRst("1");
            //随意支付方式
            body.setPayType("");
            //下单时上发不开通的报文
            body.setIsOpen("0");
            body.setTestFlowFlag("0");
            // 请求参数拼接
            OrderMonthlyPackageRequest request = new OrderMonthlyPackageRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            //拼装头信息
            RequestHeader header = utils.getRequestHeader(Constant.cmd_orderPackage, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(OrderMonthlyPackageRequest.class).toXml(request, "utf-8");
            logger.info("【新订购套餐】xml请求：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            logger.info("url=" + Constant.BASE_URL + ";接口名：" + Constant.cmd_orderPackage);

            logger.info("【新订购套餐】xml响应：" + result);
            OrderMonthlyPackageResponse responseObj = new JaxbUtil(OrderMonthlyPackageResponse.class).fromXml(result, "utf-8");

            String retCode = responseObj.getHeader().getResultCode();
            String resultInfo = responseObj.getHeader().getResultInfo();

            if (null != responseObj) {

                if (null != retCode && true == Constant.RET_CODE_0000.equalsIgnoreCase(retCode)) {
                    user.setOrderId(responseObj.getBody().getOrderId());
                    user.setOrderStartDate(responseObj.getBody().getOrderStartDate());
                    user.setOrderExpireDate(responseObj.getBody().getOrderExpireDate());
                    user.setRetCode(retCode);
                    user.setResultInfo(resultInfo);
                    return user;

                } else {
                    user.setRetCode(retCode);
                    user.setResultInfo(resultInfo);
                    return user;
                }
            } else {
                user.setRetCode("9999");
                user.setResultInfo("接口响应出现错误");
                return user;
            }
        } catch (Exception e) {
            user.setRetCode("9999");
            user.setResultInfo("接口请求出现异常");
            return user;

        }

    }

    /***
     * 新版的订购套餐：
     * 前提：支付成功，可订购并立即开通
     * @param deviceId
     * @param globalOrder
     * @param packageName
     * @param packageCode
     * @param packageType
     * @param orderPeriod
     * @param amount
     * @param secret
     * @param partnerCode
     * @return
     */
    public OrderPaymentManager orderPlan(String deviceId, String globalOrder, String packageName, String packageCode, String packageType, String orderPeriod, String amount, String secret, String partnerCode,
                                         String payRst, String payType, String isOpen, String testFlowFlag) {
        OrderPaymentManager user = new OrderPaymentManager();
        try {
            // 请求参数的处理
            OrderMonthlyPackageRequestBody body = new OrderMonthlyPackageRequestBody();
            body.setDeviceId(deviceId);
            body.setGlobalOrder(globalOrder);
            body.setPackageName(packageName);
            body.setPackageCode(packageCode);
            body.setPackageType(packageType);
            body.setOrderPeriod(orderPeriod);
            body.setPayAmount(amount);
            //未支付
            body.setPayRst(payRst);
            //随意支付方式
            body.setPayType(payType);
            //下单时上发不开通的报文
            body.setIsOpen(isOpen);
            body.setTestFlowFlag(testFlowFlag);
            // 请求参数拼接
            OrderMonthlyPackageRequest request = new OrderMonthlyPackageRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            //拼装头信息
            RequestHeader header = utils.getRequestHeader(Constant.cmd_orderPackage, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(OrderMonthlyPackageRequest.class).toXml(request, "utf-8");
            logger.info("【新订购套餐】xml请求：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            logger.info("url=" + Constant.BASE_URL + ";接口名：" + Constant.cmd_orderPackage);

            logger.info("【新订购套餐】xml响应：" + result);

            OrderMonthlyPackageResponse responseObj = new JaxbUtil(OrderMonthlyPackageResponse.class).fromXml(result, "utf-8");

            if (null != responseObj) {

                if (null != responseObj.getHeader().getResultCode() && true == Constant.RET_CODE_0000.equalsIgnoreCase(responseObj.getHeader().getResultCode())) {

                    user.setRetCode(responseObj.getHeader().getResultCode());
                    user.setResultInfo(responseObj.getHeader().getResultInfo());
                    user.setOrderId(responseObj.getBody().getOrderId());
                    user.setOrderStartDate(responseObj.getBody().getOrderStartDate());
                    user.setOrderExpireDate(responseObj.getBody().getOrderExpireDate());
                    return user;

                } else {
                    user.setRetCode(responseObj.getHeader().getResultCode());
                    user.setResultInfo(responseObj.getHeader().getResultInfo());
                    return user;
                }
            } else {
                user.setRetCode("9999");
                user.setResultInfo("接口响应出现错误");
                return user;
            }
        } catch (Exception e) {
            user.setRetCode("9999");
            user.setResultInfo("接口请求出现异常");
            return user;

        }
    }

    /**
     * Demo 批量去北京下实际的单时的实现类
     *
     * @author lm
     * @date 2018/08/31
     */
    public OrderPaymentManager batchOrderPackage(List<RequestDeviceList> deviceList, String globalOrder, String packageName, String packageCode, String packageType, String orderPeriod, String amount, String secret, String partnerCode) {
        return null;
    }


}
